﻿using System;
using System.Linq;
using WeiboPlus.Models;
using WeiboPlus.Service;

namespace WeiboPlus.Service
{
    public abstract class UserServiceBase : SNSServiceBase<SNSUser>, IUserService
    {
        protected string GET_CURRENT_USER;
        protected string GET_USER;
        protected string FOLLOW_USER;
        protected string UNFOLLOW_USER;
        protected string FRIENDSHIP;
        protected string GET_USER_LIST;
        protected string GET_FANS;
        protected string GET_FANS_LITE;
        protected string GET_IDOLS;
        protected string GET_IDOLS_LITE;
        protected string GET_BLACK_LIST;
        protected string GET_USER_FANS;
        protected string GET_USER_IDOLS;
        protected string GET_USER_SPECIALS;
        protected string SEARCH_USER;


        public UserServiceBase()
        {
            Initialize();
        }

        protected abstract void Initialize();

        public UserServiceBase(SNSAccount account)
        {
            Account = account;
        }


        public virtual void GetUser(SNSRequest snsRequest, Action<SNSUser, SNSResult> action)
        {
            var pList = GetParamtersList(snsRequest.Type);
            snsRequest.Predicate = (p) => pList.Contains(p.Name);

            PerformRequest(action, snsRequest);
        }

        public virtual void GetUserList(SNSRequest snsRequest, Action<System.Collections.Generic.IList<SNSUser>, SNSResult> action)
        {
            var pList = GetParamtersList(snsRequest.Type);
            snsRequest.Predicate = (p) => pList.Contains(p.Name);

            PerformRequest(action, snsRequest);
        }

        public virtual void Follow(SNSUser user, Action<SNSUser, SNSResult> action)
        {
            SNSRequest request = new SNSRequest() { Type = SNSType.Follow, WebMethod = Hammock.Web.WebMethod.Post };

            request.Parameters.Add(new SNSParameter(SNSParametersConstants.USER_ID, user.Id + ""));
            request.Parameters.Add(new SNSParameter(SNSParametersConstants.NAME, user.AccountName + ""));
            request.Parameters.Add(new SNSParameter(SNSParametersConstants.CLIENT_IP, "127.0.0.1"));

            var pList = GetParamtersList(request.Type);
            request.Predicate = (p) => pList.Contains(p.Name);

            PerformRequest(action, request);
        }

        public virtual void Unfollow(SNSUser user, Action<SNSUser, SNSResult> action)
        {
            SNSRequest request = new SNSRequest() { Type = SNSType.UnFollow, WebMethod = Hammock.Web.WebMethod.Post };

            request.Parameters.Add(new SNSParameter(SNSParametersConstants.USER_ID, user.Id + ""));
            request.Parameters.Add(new SNSParameter(SNSParametersConstants.NAME, user.AccountName + ""));
            request.Parameters.Add(new SNSParameter(SNSParametersConstants.CLIENT_IP, "127.0.0.1"));

            var pList = GetParamtersList(request.Type);
            request.Predicate = (p) => pList.Contains(p.Name);

            PerformRequest(action, request);
        }

        public void GetFriendship(SNSUser user, Action<SNSFriendship, SNSResult> action)
        {
            SNSRequest request = new SNSRequest() { Type = SNSType.FriendShip };

            request.Parameters.Add(new SNSParameter(SNSParametersConstants.TARGET_ID, user.Id + ""));
            //request.Parameters.Add(new SNSParameter(SNSParametersConstants.NAME, user.AccountName + ""));
            //request.Parameters.Add(new SNSParameter(SNSParametersConstants.CLIENT_IP, "127.0.0.1"));

            var pList = GetParamtersList(request.Type);
            request.Predicate = (p) => pList.Contains(p.Name);

            PerformRequest(action, request);
        }

        protected override bool TryMappingMethodString(SNSRequest snsRequest)
        {
            bool result;
            string method = string.Empty;
            switch (snsRequest.Type)
            {
                case SNSType.FriendShip:
                    method = FRIENDSHIP;
                    break;
                case SNSType.CurrentUser:
                    method = GET_CURRENT_USER;
                    break;
                case SNSType.Followers:
                    method = GET_FANS;
                    break;

                case SNSType.Friends:
                    method = GET_IDOLS;
                    break;

                case SNSType.UserFollowers:
                    method = GET_USER_FANS;
                    break;

                case SNSType.UserFriends:
                    method = GET_USER_IDOLS;
                    break;
                case SNSType.SingleUser:
                    method = GET_USER;
                    break;
                case SNSType.Follow:
                    method = FOLLOW_USER;
                    break;
                case SNSType.UnFollow:
                    method = UNFOLLOW_USER;
                    break;
                case SNSType.SearchUser:
                    method = SEARCH_USER;
                    break;
                default:
                    break;
            }
            if (string.IsNullOrEmpty(method))
            {
                result = false;
            }
            else
            {
                snsRequest.Method = method;
                result = true;
            }
            return result;
        }

        public virtual void SearchUser(SNSRequest snsRequest, Action<System.Collections.Generic.IList<SNSUser>, SNSResult> action)
        {
            //throw new NotImplementedException();
            var pList = GetParamtersList(snsRequest.Type);
            snsRequest.Predicate = (p) => pList.Contains(p.Name);

            PerformRequest(action, snsRequest);
        }
    }
}
